package find_way;

import java.util.*;

public class QueueMap {
    //优先队列的remove效率O（n） 可以用最小堆+记录id->index的map去删除元素，时间复杂的O(log n)
    public PriorityQueue<Node> openSet = new PriorityQueue<>(Comparator.comparingInt(o -> o.g + o.h));
    public Map<Integer, Node> openNodes = new HashMap<>();

    public int size() {
        return openSet.size();
    }

    public Node poll() {
        return openSet.poll();
    }

    public void addOrUpdate(int nodeId, Node node) {
        if (openNodes.containsKey(nodeId)) {
            Node existNode = openNodes.get(nodeId);
            if (node.g + node.h < existNode.g + existNode.h) {
                openSet.remove(existNode);
                openNodes.put(nodeId, node);
                openSet.add(node);
            }
        } else {
            openNodes.put(nodeId, node);
            openSet.add(node);
        }
    }
}
